package co.yixiang.hw.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.hw.domain.YxStoreCart;
import co.yixiang.hw.service.YxStoreCartService;
import co.yixiang.hw.service.dto.YxStoreCartDto;
import co.yixiang.hw.vo.StoreCartAddVo;
import co.yixiang.hw.vo.StoreCartUpdateVo;
import co.yixiang.hw.vo.YxStoreCartListVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;

/**
* @author Walert
* @date 2020-02-13
*/
@Api(tags = "购物车接口API管理")
@RestController
@RequestMapping("/api/yxStoreCart")
public class YxStoreCartController extends BaseController {

    private final YxStoreCartService yxStoreCartService;

    public YxStoreCartController(YxStoreCartService yxStoreCartService) {
        this.yxStoreCartService = yxStoreCartService;
    }

    @Log("查询购物车接口API")
    @ApiOperation(value = "查询购物车接口API",response = YxStoreCartListVo.class)
    @PostMapping(value = "/list")
    @AnonymousAccess
    public ResponseEntity<Object> getYxStoreCarts(@RequestBody JSONObject params){
        Long uid = getUserId();
        return new ResponseEntity<>(yxStoreCartService.queryUserAll(uid),HttpStatus.OK);
    }

    @Log("新增购物车接口API")
    @ApiOperation(value = "新增购物车接口API",response = YxStoreCartDto.class)
    @PostMapping(value = "/add")
//    @AnonymousAccess
    public ResponseEntity<Object> create(@Validated @RequestBody StoreCartAddVo storeCart){
        Integer addTime = (int) (System.currentTimeMillis() / 1000);
        Long userId = getUserId();
        YxStoreCart resources = new YxStoreCart();
        resources.setUid(userId.intValue());
        resources.setCartNum(storeCart.getCartNum());
        resources.setProductId(storeCart.getProductId());
        resources.setAddTime(addTime);
        resources.setIsDel(0);
        resources.setType("product");
        resources.setIsPay(0);
        resources.setIsNew(1);
        resources.setIsCheck(0);
        return new ResponseEntity<>(yxStoreCartService.create(resources),HttpStatus.OK);
    }

    @Log("修改购物车接口API")
    @ApiOperation(value = "修改购物车接口API",response = YxStoreCartDto.class)
    @PostMapping(value = "/update")
//    @AnonymousAccess
    public ResponseEntity<Object> update(@Validated @RequestBody StoreCartUpdateVo storeCart){
        YxStoreCart resources = new YxStoreCart();
        resources.setId(storeCart.getId());
        resources.setProductId(storeCart.getProductId());
        resources.setCartNum(storeCart.getCartNum());
        resources.setIsCheck(storeCart.getIsCheck());
        yxStoreCartService.update(resources);
        return new ResponseEntity<>(yxStoreCartService.findById(storeCart.getId()),HttpStatus.OK);
    }

    @Log("删除购物车接口API")
    @ApiOperation("删除购物车接口API")
    @PostMapping(value = "/delete")
//    @AnonymousAccess
    public ResponseEntity<Object> deleteAll(@RequestBody JSONObject params) {
        JSONArray ids = params.getJSONArray("ids");
        yxStoreCartService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}